package com.hxd.tabactivityfragment.common

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.view.View
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.hjq.toast.ToastUtils
import com.vondear.rxui.view.dialog.RxDialogShapeLoading

/*
 *  项目名：    Tab+Activity+Fragment
 *  包名：      com.hxd.tabactivityfragment.common
 *  文件名:     MyActivity
 *  创建者:     HXD
 *  创建时间:   2019/3/7 19:29
 *  描述:       项目中的 Activity 基类
 */

abstract class MyActivity :UIActivity(), OnTitleBarListener {

    var rxDialogShapeLoading: RxDialogShapeLoading? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWritePermission()
    }

    override fun initLayout() {
        super.initLayout()

        // 初始化标题栏的监听
        if (getTitleBarId() > 0) {
            if (findViewById<View>(getTitleBarId()) is TitleBar) {
                (findViewById<TitleBar>(getTitleBarId())).onTitleBarListener = this
            }
        }
    }

    //动态获取多媒体权限
    private fun requestWritePermission() {
        if (ActivityCompat.checkSelfPermission(
                getActivity(),
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(getActivity(), arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
        }
    }

    override fun initView() {
        super.initView()
        rxDialogShapeLoading = RxDialogShapeLoading(this)
        rxDialogShapeLoading!!.setLoadingText(setLoadingText())
    }

    //设置Loading中的文字
    open fun setLoadingText():String? = null

    /**
     * 设置标题栏的标题
     */
    override fun setTitle(titleId: Int) {
        title = getText(titleId)
    }
    /**
     * 设置标题栏的标题
     */
    override fun setTitle(title: CharSequence) {
        super.setTitle(title)
        val titleBar = getTitleBar()
        if (titleBar != null) {
            titleBar.title = title
        }
    }

    open fun getTitleBar(): TitleBar? {
        return if (getTitleBarId() > 0 && findViewById<View>(getTitleBarId()) is TitleBar) {
            findViewById(getTitleBarId())
        } else null
    }

    //返回true表示黑色字体
    override fun statusBarDarkFont(): Boolean = true


    // 标题栏左边的View被点击了
    override fun onLeftClick(v: View?) {
        onBackPressed()
    }

    // 标题栏中间的View被点击了
    override fun onTitleClick(v: View?) {

    }

    // 标题栏右边的View被点击了
    override fun onRightClick(v: View?) {

    }

    /**
     * 显示吐司
     */
    fun toast(s: CharSequence) {
        ToastUtils.show(s)
    }

    fun toast(id: Int) {
        ToastUtils.show(id)
    }

    fun toast(`object`: Any) {
        ToastUtils.show(`object`)
    }

}